<?php

/**
 * @Author: Wang Chunsheng 2192138785@qq.com
 * @Date:   2020-03-27 14:28:25
 * @Last Modified by:   Wang Chunsheng 2192138785@qq.com
 * @Last Modified time: 2020-03-28 23:51:58
 */


namespace common\services\backend;

use Yii;
use yii\web\NotFoundHttpException;
use yii\web\UnprocessableEntityHttpException;
use yii\helpers\Json;
use common\services\BaseService;
use common\helpers\ArrayHelper;
use diandi\admin\components\MenuHelper;
use diandi\admin\models\MenuTop;

/**
 * Class SmsService
 * @package common\services\backend
 * @author chunchun <2192138785@qq.com>
 */
class NavService extends BaseService
{
    public function getMenu($location = '')
    {
        $allMenu = $this->allMenu();
        // $menuGroup = MenuHelper::getAssignedMenu(Yii::$app->user->id);
        // print_r($allMenu);
        $types = array_column($allMenu, 'type', 'type');
        $menucate = MenuTop::find()->where(['mark' => $types])->orderBy('sort')->asArray()->all();
        foreach ($menucate as $key => &$value) {
            $value["text"] = $value['name'];
            $value["icon"] = $value['icon'];
            $value["targetType"] = "top-nav";
            $value["url"] = "system/welcome/{$value['mark']}";
        }
        $menus = [
            'top' => $menucate,
            'left' => $allMenu
        ];

        if (in_array($location, ['top', 'left'])) {
            return json_encode($menus[$location], JSON_UNESCAPED_UNICODE);
        } else {
            return $menus;
        }
    }

    public function allMenu()
    {
        $callback = function ($menu) {
            $data = json_decode($menu['data'], true);
            $items = $menu['children'];
            $type = Yii::$app->params['plugins'];
            $return = [
                'id' => $menu['id'],
                'text' => $menu['name'],
                'order' => $menu['order'] ? $menu['order'] : 0,
                'icon' => $menu['icon'],
                'type' => $menu['type'],
                'targetType' => "iframe-tab",
                'url' => $menu['route'],
            ];
            //处理我们的配置
            if ($data) {
                isset($data['visible']) && $return['visible'] = $data['visible']; //visible
                isset($data['icon']) && $data['icon'] && $return['icon'] = $data['icon']; //icon
                //other attribute e.g. class...
                $return['options'] = $data;
            }

            //没配置图标的显示默认图标
            (!isset($return['icon']) || !$return['icon']) && $return['icon'] = 'fa fa-circle-o';
            $items && $return['children'] = $items;
            if ($type == $return['type']) {
                $return['is_show'] = 'show';
            } else {
                $return['is_show'] = 'hide';
            }
            return  $return;
        };
        $where = ['is_sys' => 'system'];
        $is_addons = false;
        $initmenus = MenuHelper::getAssignedMenu(Yii::$app->user->id, null, $callback, $where);
        $initmenu = ArrayHelper::arraySort($initmenus, 'order');
        return $initmenu;
    }
}
